<?
 require("functions.php");


function getEmployees()
{
	$database = getDB();

	$statement = "SELECT Employees.id AS id, last_name, first_name, user_id, value AS access_level FROM Employees LEFT JOIN Keyword ON access_id = Keyword.id WHERE status = '1'";

	$results = $database->query($statement);

	$database->close();
	
	return $results;
}


function updateEmployee($updatearray_pre)
{
	$database = getDB();
	
	$updatearray = arrayEscape($updatearray_pre, $database);

	$statement = "UPDATE Employees SET user_id = '$updatearray[user_id]', last_name = '$updatearray[last_name]', first_name = '$updatearray[first_name]', middle_name = '$updatearray[middle_name]', access_id = '$updatearray[access_index]' WHERE id = '$updatearray[id]'";

	$results = $database->query($statement);
	
	$error = $database->error;
	
	$database->close();
	
	return $error == "" ? true : array('SQL Error'=> $error);	
}

function getEmployeeInfo($id)
{
	$database = getDB();

	$statement = "SELECT Employees.id AS id, user_id, last_name, first_name, middle_name, status, access_id, Keyword.value AS access_level FROM Employees LEFT JOIN Keyword ON access_id = Keyword.id  WHERE Employees.id = '$id'";

	$results = $database->query($statement);

	$database->close();
	
	return $results;
}

function addEmployee($array_with_new_info)
{
	$database = getDB();
	
	$array_with_new_info = arrayEscape($array_with_new_info, $database);
	
	$result = $database->query("SELECT * FROM Employees WHERE user_id='$array_with_new_info[user_id]'");

	if($result->num_rows != 0)
	{
		$error = "User Already Exists";
	}
	else
	{
		$statement = "INSERT INTO Employees (user_id, last_name, first_name, middle_name, access_id, status) VALUES ('$array_with_new_info[user_id]', '$array_with_new_info[last_name]', '$array_with_new_info[first_name]', '$array_with_new_info[middle_name]', '$array_with_new_info[access_index]', '1')";

		$results = $database->query($statement);
	
		$error = $database->error;
	}

	$database->close();
	
	return $error == "" ? true : array('SQL Error'=> $error);
	
}

function deleteEmployee($id)
{
	$database = getDB();

	$statement = "DELETE FROM Employees WHERE id = '$id'";

	$results = $database->query($statement);

	$database->close();
	
	return $results;
}

?>